package org.grow.jssp.controller;

import org.grow.jssp.entity.JsspFile;
import org.grow.jssp.service.FileLambdaOps;
import org.grow.jssp.service.FileOps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
public class DownloadController {
    @Value("${target}")
    private String target;
    @Autowired
    private FileOps fileOps;
    @Autowired
    private FileLambdaOps fileLambdaOps;

    //访问待下载的文件页面
    @GetMapping("/repository")
    public ModelAndView repository(HttpServletRequest request, HttpServletResponse response){
//        String target=System.getProperty("user.home");
//        target="D:\\kwzsb";
        System.out.println(target);
//        File file=new File(target);
//        File[] oldList=file.listFiles();
//        //过滤掉文件夹
//        List<File> newList=new ArrayList<File>();
//        for (int i = 0; i < oldList.length; i++) {
//            File item=oldList[i];
//            if(item.isFile()){
//                newList.add(item);
//            }
//        }
////        System.out.println(Arrays.toString(list));
//        //映射f(x)
//        ArrayList<JsspFile> finalList=new ArrayList<>();
//        for (int i = 0; i < newList.size(); i++) {
//            File item=newList.get(i);
//            JsspFile jsspFile=new JsspFile();
//            jsspFile.setTitle(item.getName());
//            jsspFile.setSize((int)item.length());
//            finalList.add(jsspFile);
//        }
//        System.out.println(newList);

//        List<JsspFile> finalList=this.fileOps.ops(target);
        List<JsspFile> finalList=this.fileLambdaOps.ops(target);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("repository");
        modelAndView.addObject("fileToDownload",finalList);
        return modelAndView;
    }

    @GetMapping("/download/{filename}")
    public void download(@PathVariable String filename,
                         HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("将要下载："+filename);
//        String target=System.getProperty("user.home");
//        target="D:\\kwzsb";
        System.out.println(target);
        String t =target+File.separator+filename;
        System.out.println(t);
        long length=new File(t).length();
        //准备下载
        //告诉浏览器内容类型不是html而是二进制流
        response.setContentType("application/octet-stream");
//        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.addHeader("Content-Disposition",
                "attachment;filename="+t);
//        ContentDisposition c=ContentDisposition.attachment().filename(filename).build();
//        response.addHeader(HttpHeaders.CONTENT_DISPOSITION,c.toString());
        response.setContentLength((int)length);
        byte[] buffer=new byte[1024000];
        BufferedInputStream in=new BufferedInputStream(new FileInputStream(t));
        BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream());
//        int r=in.read(buffer);
//        out.write(buffer,0,r);
//        int r;
//        while ((r=in.read(buffer))!=-1){
//            out.write(buffer,0,r);
//        }
        while (true){
            int r=in.read(buffer);
            if (r==-1){
                break;
            }
            out.write(buffer,0,r);
        }
//        response.setContentType();
//        response.flushBuffer();
        out.flush();
    }
}
